﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
using LotteryVote.Data;
using System.Web;
using LotteryVote.Entity;

namespace LotteryVote.Logic.Bet
{
    internal abstract class DigitMultipleCalculator : MultipleCalculator
    {

        private GamePlayWayDataAccess _daGamePlayWay;
        internal GamePlayWayDataAccess DaGamePlayWay
        {
            get
            {
                if (_daGamePlayWay == null)
                    _daGamePlayWay = new GamePlayWayDataAccess();
                return _daGamePlayWay;
            }
            set
            {
                _daGamePlayWay = value;
            }
        }


        protected override abstract IDictionary<int, IEnumerable<string>> LotteryResult { get; }

        public abstract override int Calculate(Entity.BetOrder order);

        protected virtual void CheckNumLength(string num, int len)
        {
            if (num.Length != len)
                throw new ArgumentException("num's length not equals " + len);
        }
        protected Func<string, int, int> TransToMethod(string wayName)
        {
            wayName = wayName.Replace("+", "And");
            string methodName = "Calc" + wayName;
            var method = this.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
            ParameterExpression numExp = Expression.Parameter(typeof(string), "num");
            ParameterExpression companyTypeExp = Expression.Parameter(typeof(int), "companyTypeId");
            LambdaExpression lambda = LambdaExpression.Lambda<Func<string, int, int>>(Expression.Call(Expression.Constant(this), method, numExp, companyTypeExp), numExp, companyTypeExp);
            return lambda.Compile() as Func<string, int, int>;
        }


        protected abstract int CalcHead(string num, int companyId);
        protected abstract int CalcLast(string num, int companyId);
        protected abstract int CalcHeadAndLast(string num, int companyId);
        protected abstract int CalcRoll(string num, int companyId);
        protected abstract int CalcRoll7(string num, int companyId);


        protected override abstract void ClearLotteryResult();
    }
}
